import { defineStore } from 'pinia'
import { loginApi } from '@/api/login'
import { getUserInfo } from '@/api/user'

export const useUserStore = defineStore('user', {
  state() {
    return {
      token: '',
      permissions: [] as string[], // 用户权限
      info: {} // 用户信息
    }
  },

  actions: {
    // 登录
    async login(username: string, password: string) {
      // 调接口
      const { data } = await loginApi(username, password)
      // 将 access_token 储存到 token 上
      this.token = data.access_token
    },

    // 获取信息
    async getInfo() {
      const { data } = await getUserInfo()
      this.info = data
      this.permissions = data.permissions
      return data
    }
  },

  // 启用持久化
  persist: true
  // persist: {
  //   storage: sessionStorage // 配置持久化在 sessionStorage
  // }
})
